home *** CD-ROM | disk | FTP | other *** search
/ Shareware Grab Bag / Shareware Grab Bag.iso / 090 / dlab.cq / dlab.c
Text File  |  1985-09-28  |  7KB  |  315 lines

  1. /*
  2. Program written in Lattice C 2.14 by Adam Finnefrock  6/30/85
  3. Diskette Labeller Program  -  DLAB
  4. */
  5.  
  6. #include "stdio.h"
  7. #include "dos.h"
  8. #include "util.h"
  9.  
  10.  
  11. #define DIRSIZE        200
  12. #define WIDE        5
  13. #define CONDENSED    17
  14. #define LLENGTH        1.4375
  15. #define LSKIP        2
  16. #define LLINES        (LLENGTH*12)
  17. #define    LCOLUMNS    (LCHARS/13)
  18.  
  19.  
  20. char *findfirst(),*findnext(),*strchr();
  21. char *dta,*setdta();
  22. char **dirlist();
  23. char drive;
  24. long bytesused(),bytesfree();
  25.  
  26.  
  27. main()
  28. {
  29.     char *list[DIRSIZE],label[80];
  30.     int i,files,valid=0;
  31.     FILE *fp;
  32.     float LWIDTH=4;
  33.     int LWIDECHARS,LCHARS,linesofdata,lastline;
  34.     char *s,c;
  35.  
  36.  
  37.  
  38.     dta=setdta((char *) getmem(0x80));
  39.  
  40.     printf("DISKETTE LABEL PROGRAM (DLAB)\n");
  41.     printf("    created by Adam Finnefrock\n");
  42.     if ((fp=fopen("prn:","w"))==NULL) {
  43.         printf("Printer not working.\n");
  44.         exit(1);
  45.     }
  46.  
  47.     printf("\nDrive letter? ");
  48.     while ((drive=toupper(getch())) <'A' || drive>'H')
  49.         ;
  50.     putch(drive);
  51.     printf("\nDo you have the FX-80 without the external tractor feed? ");
  52.         while (! valid) {
  53.         printf("N\b");
  54.         c=toupper(getch());
  55.         if (c=='Y' || c=='N' || c=='\r')
  56.             valid=1;
  57.     }
  58.     putch(c);
  59.         if (c=='Y')
  60.         LWIDTH=LWIDTH - 0.4;
  61.  
  62.     LWIDECHARS=LWIDTH*WIDE;
  63.     LCHARS=LWIDTH*CONDENSED;
  64.  
  65.  
  66.     printf("\n\nInput a floppy diskette in %c: and press any key",drive);
  67.     printf(" or press ESC to quit.\n");
  68.  
  69.     while (getch() !='\033') {
  70.         dirlist(list);
  71.         cls();
  72.         for (i=0; list[i] !=NULL; ++i)
  73.             ;
  74.         files=i;
  75.         sort(list,files-1);
  76.         column(list,files-1,20,stdout);
  77.         printf("\n\n");
  78.         linesofdata=(files-1)/LCOLUMNS +1;
  79.         if (linesofdata > LLINES-5) {
  80.             printf("Warning: the disk list may overrun the label size\n");
  81.             printf("Are you sure you want to print it? ");
  82.             while ((c=toupper(getch())) !='Y' && c !='N')
  83.                 ;
  84.             printf("%c\n",c);
  85.             if (c == 'N')
  86.                 goto reloop;
  87.         }
  88.  
  89.  
  90.         printf("Press ESC to skip this disk.\n");
  91.         printf("Label for disk: ");
  92.         label[0]='\0';
  93.  
  94.         if (input(label,LWIDECHARS,' ','[',']') !=NULL) {
  95.             fprintf(fp,"\033E\033G");     /* emphasized, double strike */
  96.             fprintf(fp,"\016%s\n",label);    /* wide label */
  97.             fprintf(fp,"\033F\033H");    /* no emphasized, no double strike */
  98.             fprintf(fp,"\033S0\017");    /* condensed, superscript */
  99.             fprintf(fp,"\033A\006");    /* half-line spacing */
  100.             for (i=1; i<=LCHARS; ++i)
  101.                 fputc('-',fp);
  102.             fputc('\n',fp);
  103.  
  104.             lastline=column(list,files-1,(int) max(linesofdata,LLINES-7),fp);
  105.             for (i=lastline; i<=LLINES-7; ++i)    /* go to two spaces above bottom */
  106.                 fputc('\n',fp);            /*   if not there or beyond already */
  107.             if (lastline <= LLINES-7) {        /* if room, print extra data */
  108.                 for (i=1; i<=LCHARS; ++i)
  109.                     fputc('-',fp);
  110.                 fputc('\n',fp);
  111.                 fprintf(fp,"FILES: %-6d BYTES FREE: %-10ld BYTES USED: %-10ld\n",files,bytesfree(),bytesused());
  112.             }
  113.             for (i=1; i<=LSKIP; ++i)    /* skip to new label */
  114.                 fputc('\n',fp);
  115.             fprintf(fp,"\033T\022");    /* no superscript, no condensed */
  116.             fprintf(fp,"\0332");        /* normal line spacing */
  117.         }
  118.  
  119. reloop:        printf("\n\nInsert another disk and press any key");
  120.         printf(" or press ESC to quit.\n");
  121.     }
  122.     fclose(fp);
  123.  
  124. }
  125.  
  126.  
  127. column(list,lastitem,pagelen,outfile) /* return number of lines printed */
  128. char *list[];
  129. int lastitem,pagelen;
  130. FILE *outfile;
  131. {
  132.     int i,j;
  133.  
  134.     for (i=0; i<=lastitem && i<pagelen; ++i) {
  135.         for (j=i; j<=lastitem; j+=pagelen)
  136.             fprintf(outfile,"%-13s",list[j]);
  137.         fprintf(outfile,"\n");
  138.     }
  139.     return(i);
  140. }
  141.  
  142.  
  143.  
  144. sort(list,lastitem)
  145. char *list[];
  146. int lastitem;
  147. {
  148.     int i,j;
  149.     char *temp;
  150.  
  151.     for (i=lastitem-1; i>=0; --i)
  152.         for (j=0; j<=i; ++j)
  153.             if (strcmp(list[j],list[j+1])>0) {
  154.                 temp=list[j];
  155.                 list[j]=list[j+1];
  156.                 list[j+1]=temp;
  157.             }
  158. }
  159.  
  160.  
  161.  
  162. char **dirlist(directory)
  163.                 /* put simple directory
  164.                    into sp. Returns pointer to
  165.                    where next item might go. */
  166. char *directory[];
  167. {
  168.     char *filename,*retfile,*name;
  169.  
  170.     filename=" :\\*.*";
  171.     *filename=drive;
  172.  
  173.     if ((retfile=findfirst(filename)) !=NULL) {
  174.         align(retfile);
  175.         name=(char *) getmem((strlen(retfile)+1) *sizeof(char));
  176.         if (name==NULL)
  177.             outofmem();
  178.         strcpy(name,retfile);
  179.         *directory++ = name;
  180.     }
  181.  
  182.  
  183.  
  184.     while ((retfile=findnext()) !=NULL) {
  185.         align(retfile);
  186.         name=(char *) getmem((strlen(retfile)+1) *sizeof(char));
  187.         if (name==NULL)
  188.             outofmem;
  189.         strcpy(name,retfile);
  190.         *directory++ = name;
  191.     }
  192.  
  193.     *directory=NULL;
  194.     return(directory);
  195.  
  196. }
  197.  
  198.  
  199. align(s)                /* space out extension */
  200. char *s;                /* NOTE: s should be 13 chars long */
  201. {
  202.     char *p;
  203.     int period,from,to,last,i;
  204.  
  205.     p= strchr(s,'.');
  206.     if (p !=NULL) {
  207.         last=strlen(s)-1;
  208.         period= p-s;
  209.         while (last-period<3)
  210.             s[++last]=' ';
  211.  
  212.         for (from=last, to=11; from >=period+1; --from,--to)
  213.             s[to]=s[from];
  214.         s[8]='.';
  215.         for (i=period; i<8; ++i)
  216.             s[i]=' ';
  217.         s[12]='\0';
  218.     }
  219. }
  220.  
  221.  
  222.  
  223. char *findfirst(s)            /* NOTE: may ruin s */
  224. char *s;
  225. {
  226.     union REGS *inregs;
  227.     union REGS *outregs;
  228.  
  229.     inregs->x.dx=(short) s;        /* filename */
  230.     inregs->x.cx=0;            /* atribute find all normal */
  231.     inregs->h.ah=0x4E;        /* int 21h, FIND FIRST */
  232.     intdos(inregs,outregs);
  233.     if (outregs->x.ax==2 || outregs->x.ax==18)
  234.         return(NULL);
  235.     else
  236.         return((char *) dta+30);
  237.  
  238. }
  239.  
  240. char *findnext()
  241. {
  242.     union REGS *inregs;
  243.     union REGS *outregs;
  244.  
  245.     inregs->h.ah=0x4F;        /* int 21h, FIND NEXT */
  246.     intdos(inregs,outregs);
  247.     if (outregs->x.ax==18)
  248.         return(NULL);
  249.     else
  250.         return((char *) dta+30);
  251. }
  252.  
  253. char *setdta(p)
  254. char *p;
  255. {
  256.     union REGS *inregs;
  257.     union REGS *outregs;
  258.  
  259.     inregs->x.dx=(short) p;        /* dta area */
  260.     inregs->h.ah=0x1A;        /* int 21h with 1Ah */
  261.     intdos(inregs,outregs);
  262.     return(p);
  263. }
  264.  
  265. outofmem()
  266. {
  267.     cprintf("\r\nLABEL: Out of memory.\r\n");
  268.     exit(2);
  269. }
  270.  
  271.  
  272. long bytesfree()
  273. {
  274.     union REGS *inregs;
  275.     union REGS *outregs;
  276.     int free_clusters,bytes_per_sector,sectors_per_cluster;
  277.     long free_bytes;
  278.  
  279.     inregs->h.dl=drive-'A'+1;    /* drive */
  280.     inregs->h.ah=0x36;        /* int 21h with 36h */
  281.     intdos(inregs,outregs);
  282.  
  283.     sectors_per_cluster=outregs->x.ax;
  284.     free_clusters=outregs->x.bx;
  285.     bytes_per_sector=outregs->x.cx;
  286.  
  287.     free_bytes=free_clusters;
  288.     free_bytes *= (sectors_per_cluster * bytes_per_sector);
  289.     return(free_bytes);
  290. }
  291.  
  292.  
  293. long bytesused()
  294. {
  295.     union REGS *inregs;
  296.     union REGS *outregs;
  297.     int free_clusters,bytes_per_sector,sectors_per_cluster,total_clusters;
  298.     long used_bytes;
  299.  
  300.     inregs->h.dl=drive-'A'+1;    /* drive */
  301.     inregs->h.ah=0x36;        /* int 21h with 36h */
  302.     intdos(inregs,outregs);
  303.  
  304.     sectors_per_cluster=outregs->x.ax;
  305.     free_clusters=outregs->x.bx;
  306.     bytes_per_sector=outregs->x.cx;
  307.     total_clusters=outregs->x.dx;
  308.  
  309.     used_bytes=total_clusters-free_clusters;
  310.     used_bytes *= (sectors_per_cluster * bytes_per_sector);
  311.     return(used_bytes);
  312. }
  313.  
  314.  
  315.